// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_CONNECTION_QUEUE_H_
#define SRC_CONNECTION_QUEUE_H_

#include <pthread.h>
#include <deque>

namespace seven_summits {

class ConnectionQueue {
 public:
  ConnectionQueue() {
    pthread_mutex_init(&queue_mutex_, NULL);
  }
  virtual ~ConnectionQueue() {}

  virtual void Push(Connection* c) {
    pthread_mutex_lock(&queue_mutex_);
    queue_.push_front(c);
    pthread_mutex_unlock(&queue_mutex_);
  }

  virtual Connection* Pop() {
    pthread_mutex_lock(&queue_mutex_);
    Connection* c = queue_.back();
    queue_.pop_back();
    pthread_mutex_unlock(&queue_mutex_);
    return c;
  }

 private:
  pthread_mutex_t queue_mutex_;
  std::deque<Connection*> queue_;
};

}  // namespace seven_summits

#endif // SRC_CONNECTION_QUEUE_H_
